﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Scheduling.Models.Interfaces;

namespace Scheduling.Models.Helpers
{
    public class StatisticsHelper
    {

        public enum ChartType
        {
            TopUsers, TopRooms, TopDays
        };
        public static List<User> GetTopUsers(int amount = 5)
        {
            List<User> returnList = new List<User>();
            var userLoader = IoC.Resolve<IUserLoader>();
            var allUsers = userLoader.GetUsers();
            if (allUsers.Count < amount) amount = allUsers.Count;
            Comparison<User> comparison = new Comparison<User>(CompareNumBookingsForUser);
            allUsers.Sort(comparison);

            returnList.AddRange(allUsers.GetRange(0, amount).AsEnumerable());
            return returnList;
        }

        public static List<Room> GetTopRooms(int amount = 5)
        {

            List<Room> returnList = new List<Room>();
            var roomLoader = IoC.Resolve<IRoomLoader>();
            var allRooms = roomLoader.GetRooms();

            if (allRooms.Count < amount) amount = allRooms.Count;

            Comparison<Room> comparison = new Comparison<Room>(CompareNumBookingsForRoom);
            allRooms.Sort(comparison);

            returnList.AddRange(allRooms.GetRange(0, amount).AsEnumerable());
            return returnList;
        }
        public static int GetNumBookingsForRoom(Room room)
        {
            var bookingLoader = IoC.Resolve<IBookingLoader>();
            var bookings = bookingLoader.GetBookings();
            int count = 0;

            foreach (var booking in bookings)
            {
                if (booking.Room.Name == room.Name) count++;
            }
            return count;
        }




        private static int CompareNumBookingsForUser(User a, User b)
        {
            var bookingLoader = IoC.Resolve<IBookingLoader>();
            var bookingsA = bookingLoader.GetBookingsByUserID(a.ID);
            var bookingsB = bookingLoader.GetBookingsByUserID(b.ID);

            return bookingsB.Count.CompareTo(bookingsA.Count);
        }

        private static int CompareNumBookingsForRoom(Room a, Room b)
        {
            var bookingLoader = IoC.Resolve<IBookingLoader>();
            var bookings = bookingLoader.GetBookings();
            int aCount = 0, bCount = 0;

            foreach (var booking in bookings)
            {
                if (booking.Room.ID == a.ID) aCount++;
                if (booking.Room.ID == b.ID) bCount++;
            }

            return bCount.CompareTo(aCount);
        }

        public static List<string> GenerateDayList()
        {
            var days = new List<string>();
            foreach (var s in Enum.GetValues(typeof (DayOfWeek)))
            {
                days.Add(s.ToString());
            }
            return days;
        }

        public static int[] GetBookingsPerDay(List<Booking> bookingList)
        {
            var bookingsPerDay = new int[7];
            foreach (var booking in bookingList)
            {
                switch (booking.StartDate.DayOfWeek)
                {
                    case DayOfWeek.Monday:
                        bookingsPerDay[1]++;
                        break;
                    case DayOfWeek.Tuesday:
                        bookingsPerDay[2]++;
                        break;
                    case DayOfWeek.Wednesday:
                        bookingsPerDay[3]++;
                        break;
                    case DayOfWeek.Thursday:
                        bookingsPerDay[4]++;
                        break;
                    case DayOfWeek.Friday:
                        bookingsPerDay[5]++;
                        break;
                    case DayOfWeek.Saturday:
                        bookingsPerDay[6]++;
                        break;
                    case DayOfWeek.Sunday:
                        bookingsPerDay[0]++;
                        break;
                }
            }

            return bookingsPerDay;
        }
    }
}